package org.example.carsystem.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;

import org.example.carsystem.domain.Menu;
import org.example.carsystem.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private HttpServletRequest request;

    @GetMapping("list")
    public Object list(@RequestParam(value = "menuName" ,required = false) String menuName,
                       @RequestParam(value = "status",required = false) String status){
        System.err.println(status);
        List<Menu> lists = menuService.lists(menuName, status);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",lists);
        return map;
    }
    @RequestMapping("listss")
    public Map<String,Object> lists(){
        Map<String,Object> map=    menuService.listss();
        System.out.println(map.get("data"));
        return map;
    }
    @RequestMapping("add")
    public Map<String,Object> list(@RequestBody Menu menu){
        HashMap<String, Object> map = new HashMap<>();
        Integer i=  menuService.adds(menu);
        map.put("code",200);
        map.put("data","添加成功");
        return map;

    }

    @RequestMapping("getbyid/{menuId}")
    public Map<String,Object> updata(@PathVariable("menuId") Integer menuId){
        HashMap<String, Object> map = new HashMap<>();

        Menu menu=  menuService.getbyid(menuId);
      map.put("data",menu);
      map.put("code","查询一条数据");
      return map;
    }

    @RequestMapping("update")
    public Map<String,Object> updata(@RequestBody Menu menu){
        HashMap<String, Object> map = new HashMap<>();
       Integer updataid=  menuService.update(menu);
       map.put("data","修改成功");
       map.put("code","200");
       return map;
    }

    @RequestMapping("lookselecttree")
    public List<Menu> fuzzysearch(@RequestParam(value = "menuName",required = false) String menuName){
      List<Menu> list= menuService.fuzzhsearch(menuName);
      return list;
    }
    @RequestMapping("deletebyid/{menuId}")
    public Map<String,Object> deletebyid(@PathVariable("menuId") Integer menuId){
        boolean deletebyidss = menuService.deletebyidss(menuId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",deletebyidss);
        map.put("code","200");
        return map;
    }

    @RequestMapping("lookmenu")
    public Map<String,Object> lookmenu(){
        HashMap<String, Object> map = new HashMap<>();
//        String token = request.getHeader("token");
//        JWT jwt = JWTUtil.parseToken(token);
//        Integer userId =(Integer) jwt.getPayload("userId");
        Integer userId=2;
        List<Menu> listss=  menuService.lookselect(userId);
        map.put("data",listss);
        return map;
    }



}
